using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SmartMathLibrary.QuadPack
{
    /// <summary>
    /// The Gauss-Kronrod rule 15.
    /// </summary>
    [Serializable]
    public static class Dqk15Class
    {
        /// <summary>
        /// The Gauss-Kronrod rule 15.
        /// </summary>
        /// <param name="f">The function.</param>
        /// <param name="a">The lower limit of integration.</param>
        /// <param name="b">The upper limit of integration.</param>
        /// <param name="abserr">Estimate of the modulus of the absolute error, which should not exceed abs(i-result).</param>
        /// <param name="resabs">Approximation to the integral j.</param>
        /// <param name="resasc">Approximation to the integal of abs(f-i/(b-a)) over (a,b).</param>
        /// <returns>Approximation to the integral.</returns>
        public static double G_K15(HardRealFunction f, double a, double b, ref double abserr, ref double resabs,
                                   ref double resasc)
        {
            double[] XGK15 = {
                                 0.99145537112081263921, 0.94910791234275852453, 0.86486442335976907279,
                                 0.74153118559939443986, 0.58608723546769113029, 0.40584515137739716691,
                                 0.20778495500789846760, 0.00000000000000000000
                             };
            double[] WGK15 = {
                                 0.02293532201052922496, 0.06309209262997855329, 0.10479001032225018384,
                                 0.14065325971552591875, 0.16900472663926790283, 0.19035057806478540991,
                                 0.20443294007529889241, 0.20948214108472782801
                             };
            double[] WG7 = {
                               0.12948496616886969327, 0.27970539148927666790, 0.38183005050511894495,
                               0.41795918367346938776
                           };

            double[] fv1 = new double[7];
            double[] fv2 = new double[7];
            double absc;
            double centr;
            double dhlgth;
            double fc;
            double fsum;
            double fval1;
            double fval2;
            double hlgth;
            double resg;
            double resk;
            double reskh;
            double result;
            int j;
            int jtw;
            int jtwm1;

            centr = 0.5 * (a + b);
            hlgth = 0.5 * (b - a);
            dhlgth = Math.Abs(hlgth);

            fc = f.SolveAt(centr);
            resg = fc * WG7[3];
            resk = fc * WGK15[7];
            resabs = Math.Abs(resk);
            for (j = 0; j < 3; j++)
            {
                jtw = 2 * j + 1;
                absc = hlgth * XGK15[jtw];
                fval1 = f.SolveAt(centr - absc);
                fval2 = f.SolveAt(centr + absc);
                fv1[jtw] = fval1;
                fv2[jtw] = fval2;
                fsum = fval1 + fval2;
                resg += WG7[j] * fsum;
                resk += WGK15[jtw] * fsum;
                resabs = resabs + WGK15[jtw] * (Math.Abs(fval1) + Math.Abs(fval2));
            }
            for (j = 0; j < 4; j++)
            {
                jtwm1 = j * 2;
                absc = hlgth * XGK15[jtwm1];
                fval1 = f.SolveAt(centr - absc);
                fval2 = f.SolveAt(centr + absc);
                fv1[jtwm1] = fval1;
                fv2[jtwm1] = fval2;
                fsum = fval1 + fval2;
                resk = resk + WGK15[jtwm1] * fsum;
                resabs = (resabs) + WGK15[jtwm1] * (Math.Abs(fval1) + Math.Abs(fval2));
            }
            reskh = resk * 0.5;
            resasc = WGK15[7] * Math.Abs(fc - reskh);
            for (j = 0; j < 7; j++)
            {
                resasc = (resasc) + WGK15[j] * (Math.Abs(fv1[j] - reskh) + Math.Abs(fv2[j] - reskh));
            }
            result = resk * hlgth;
            resabs = (resabs) * dhlgth;
            resasc = (resasc) * dhlgth;
            abserr = Math.Abs((resk - resg) * hlgth);
            if ((resasc != 0.0) && (abserr != 0.0))
            {
                abserr = (resasc) *
                         (((1.0) < (Math.Pow((200.0 * (abserr) / (resasc)), 1.5)))
                              ? (1.0)
                              : (Math.Pow((200.0 * (abserr) / (resasc)), 1.5)));
            }
            if (resabs > Double.MinValue / (50.0 * Double.Epsilon))
            {
                abserr = (((Double.Epsilon * 50.0 * (resabs)) > ((abserr)))
                              ? (Double.Epsilon * 50.0 * (resabs))
                              : ((abserr)));
            }
            return result;
        }
    }
}